package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.BaseEvent;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.ShopCarMoney;
import com.mvp.shq.washer.entity.shop.TotalOrderList;
import com.mvp.shq.washer.entity.shop.TotalOrderListData;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.rxbus.BusData;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.DBUtil;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.StringUtils;
import com.mvp.shq.washer.view.widget.CustomDialog;
import com.orhanobut.logger.Logger;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by westing on 2017/10/11.
 * 我的多商品订单列表
 */

public class ShopCarAllListAdapter extends RecyclerView.Adapter<ShopCarAllListAdapter.ViewHolder> {


    private Context context;

    public List<TotalOrderListData> getmTotalOrderListData() {
        return mTotalOrderListData;
    }

    public void setmTotalOrderListData(List<TotalOrderListData> mTotalOrderListData) {
        this.mTotalOrderListData = mTotalOrderListData;
    }

    private List<TotalOrderListData> mTotalOrderListData;
    private MyAllOrderItemOnClick myAllOrderItemOnClick;
    private CustomDialog mDialog;
    private boolean isSetting;
    private boolean isChecking;
    private double mTotalMoney;
    private int mTotalIntegral;
    private List<ShopcarlistAdapter>  adapterList=new ArrayList<>();
    private List<ShopcarsetAdapter>  adapterSetList=new ArrayList<>();
    private List<ViewHolder>  mViewHolderList=new ArrayList<>();
    public int getmTotalIntegral() {
        return mTotalIntegral;
    }

    public void setmTotalIntegral(int mTotalIntegral) {
        this.mTotalIntegral = mTotalIntegral;
    }

    public double getmTotalMoney() {
        return mTotalMoney;
    }

    public void setmTotalMoney(double mTotalMoney) {
        this.mTotalMoney = mTotalMoney;
    }

    public boolean isChecking() {
        return isChecking;
    }

    public void setChecking(boolean checking) {
        isChecking = checking;
    }

    public ShopCarAllListAdapter(Context mContext, List<TotalOrderListData> totalOrderListData, MyAllOrderItemOnClick myAllOrderItemOnClick, boolean isSetting) {
        context = mContext;
        this.mTotalOrderListData = totalOrderListData;
        this.myAllOrderItemOnClick = myAllOrderItemOnClick;
        this.isSetting=isSetting;
    }

    public boolean isSetting() {
        return isSetting;
    }

    public void setSetting(boolean setting) {
        isSetting = setting;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_allshopcar, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        mViewHolderList.add(holder);
        holder.recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 4, ContextCompat.getColor(context, R.color.gregf8));
        holder.recyclerview.addItemDecoration(divider);

        if (mTotalOrderListData == null) return;
        TotalOrderListData totalOrderListData = mTotalOrderListData.get(position);
        if (totalOrderListData == null) return;
        List<Product> productlist = totalOrderListData.getProducts();
        if (productlist == null) return;

        ShopcarlistAdapter  listadapter= new ShopcarlistAdapter(context, productlist, new ShopcarlistAdapter.CheckLiserent() {

            @Override
            public void itemClick(int childposition) {
                myAllOrderItemOnClick.onItemOnClick(productlist.get(childposition).getId());
            }

            @Override
            public void onCheckItem(int childPosition) {
                boolean b = mTotalOrderListData.get(position).getProducts().get(childPosition).isCheck();
                mTotalOrderListData.get(position).getProducts().get(childPosition).setCheck(!b);
                BusData data = new BusData();
                data.setPosition(position);
                RxBus.get().send(EventCode.SHOP_CART_UPDATE_COMPANY_LIST,data);
                if (!b){
                    //当前选中
                    RxBus.get().send(EventCode.SHOP_CART_CHECK,mTotalOrderListData.get(position).getProducts().get(childPosition));
                }else {
                    RxBus.get().send(EventCode.SHOP_CART_CHECKNO,mTotalOrderListData.get(position).getProducts().get(childPosition));
                }
            }
        });
        adapterList.add(listadapter);
        ShopcarsetAdapter setadapter=new ShopcarsetAdapter(context, productlist, new ShopcarsetAdapter.ClickSetNumLiserent() {
            @Override
            public void changeNum(int num,int childrenPosition) {
                if (num == 0){
                    DBUtil.getInstance().delProduct(productlist.get(childrenPosition));
                    productlist.remove(childrenPosition);
                }else {
                    productlist.get(childrenPosition).setByNum(num);
                    DBUtil.getInstance().UpdateProduct(productlist.get(childrenPosition));
                }
                RxBus.get().send(EventCode.SHOP_CART_ADD,new TotalOrderList(mTotalOrderListData,position));
            }

            @Override
            public void delete(int num) {
                DBUtil.getInstance().delProduct(productlist.get(position));
                mTotalOrderListData.get(position).getProducts().remove(num);
                RxBus.get().send(EventCode.SHOP_CART_DEL_PRODUCT,new TotalOrderList(mTotalOrderListData,position));
            }

            @Override
            public void itemOnClick(int childposition) {
                myAllOrderItemOnClick.onItemOnClick(productlist.get(childposition).getId());
            }
            @Override
            public void onCheckItem(int childPosition) {
                boolean b = mTotalOrderListData.get(position).getProducts().get(childPosition).isCheck();
                mTotalOrderListData.get(position).getProducts().get(childPosition).setCheck(!b);
                BusData data = new BusData();
                data.setPosition(position);
                RxBus.get().send(EventCode.SHOP_CART_UPDATE_COMPANY_LIST,data);
                if (!b){
                    //当前选中
                    RxBus.get().send(EventCode.SHOP_CART_CHECK,mTotalOrderListData.get(position).getProducts().get(childPosition));
                }else {
                    RxBus.get().send(EventCode.SHOP_CART_CHECKNO,mTotalOrderListData.get(position).getProducts().get(childPosition));
                }
            }


        });
        adapterSetList.add(setadapter);

        setadapter.setCheckAll(isChecking);
        listadapter.setCheckAll(isChecking);

        if(isSetting){
            holder.recyclerview.setAdapter(setadapter);
        }else{
            holder.recyclerview.setAdapter(listadapter);
        }

        holder.llMoney.setVisibility(View.VISIBLE);
        holder.tvAdd.setVisibility(View.VISIBLE);
        holder.llIntegral.setVisibility(View.VISIBLE);
        holder.tvAdd.setVisibility(View.VISIBLE);
        setTotalMoney(position);
    }

    private void setTotalMoney(int position){
        List<Product> productlist=mTotalOrderListData.get(position).getProducts();
        if(productlist==null || productlist.size() == 0){
            mViewHolderList.get(position).linBottom.setVisibility(View.GONE);
            return;
        }
        double money1 =0;
        int integral=0;
        for(int i=0,len=productlist.size();i<len;i++){
            Product product= productlist.get(i);
            money1+=product.getCashPrice()*product.getByNum();//单价*数量
            integral+=product.getIntegralPrice()*product.getByNum();
        }
        String money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(money1));
        mViewHolderList.get(position).tvMoney1.setText((int) money1 + "");
        mViewHolderList.get(position).tvMoney2.setText(money2);
        mViewHolderList.get(position).tvIntegral.setText((int) integral + "");

        if (money1 == 0 && money2.equals(0)) {
            mViewHolderList.get(position).tvAdd.setVisibility(View.GONE);
            mViewHolderList.get(position).tvMoney1.setVisibility(View.GONE);
            mViewHolderList.get(position).tvMoney2.setVisibility(View.GONE);
        }
        else if (integral == 0) {
            mViewHolderList.get(position).llIntegral.setVisibility(View.GONE);
            mViewHolderList.get(position).tvAdd.setVisibility(View.GONE);
        }else{
            mViewHolderList.get(position).tvMoney1.setVisibility(View.VISIBLE);
            mViewHolderList.get(position).tvMoney2.setVisibility(View.VISIBLE);
            mViewHolderList.get(position).tvAdd.setVisibility(View.VISIBLE);
            mViewHolderList.get(position).llIntegral.setVisibility(View.VISIBLE);
        }

    }
    public void updateUI(){
        for (int i=0,len=adapterSetList.size();i<len;i++){
            adapterSetList.get(i).setCheckAll(isChecking);
            adapterSetList.get(i).notifyDataSetChanged();
        }

        for (int i=0,len=adapterList.size();i<len;i++){
            adapterList.get(i).setCheckAll(isChecking);
            adapterList.get(i).notifyDataSetChanged();
        }

    }

    public void updateUIView(int position){
        //adapterSetList.get(position).setProductlist(mTotalOrderListData.get(position).getProducts());
        //adapterList.get(position).setDatas(mTotalOrderListData.get(position).getProducts());
        setTotalMoney(position);
        adapterList.get(position).notifyDataSetChanged();
        adapterSetList.get(position).notifyDataSetChanged();
    }

    public void updateCompanyList(int position){
        if (adapterSetList == null || adapterSetList.size() <= 0){
            return;
        }
        adapterList.get(position).notifyDataSetChanged();
        adapterSetList.get(position).notifyDataSetChanged();
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getItemCount() {
        return mTotalOrderListData != null ? mTotalOrderListData.size() : 0;
    }



    class ViewHolder extends RecyclerView.ViewHolder {
//        @BindView(R.id.tv_orderid)
//        TextView tvOrderid;
        @BindView(R.id.recyclerview)
        RecyclerView recyclerview;
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;//实付元
        @BindView(R.id.tv_money2)
        TextView tvMoney2;//实付角分
        @BindView(R.id.ll_money)
        LinearLayout llMoney;//实付金额
        @BindView(R.id.tv_add)
        TextView tvAdd;//加号
        @BindView(R.id.tv_integral)
        TextView tvIntegral;//积分
        @BindView(R.id.tv_ji_fen)
        TextView tvJiFen;
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;//实付积分
        @BindView(R.id.lin_item)
        LinearLayout linItem;
        @BindView(R.id.lin_bottom)
        LinearLayout linBottom;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface MyAllOrderItemOnClick {
        void onItemOnClick(String productId);
    }
}
